﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;


public class AndroidControl : MonoBehaviour
{
    private AndroidJavaObject jo;

    private Button btn_wap;
    private Button btn_sdk;
    private Button btn_wap_onestep;
    private Button btn_sdk_onestep;

    private Button btn_wap_url;
    private Button btn_sdk_url;
    private Button btn_wap_onestep_url;
    private Button btn_sdk_onestep_url;

    private Button btn_changePayType;
    private Button btn_payBrower;

    private Text text_PayType;
    private Text labelUrlType;
    private Text labelPayStatus;
    private int w;
    private int h;
    //Handle the submission parameters, tune up the wallet application
    private String BaseApi = "http://123ttd1.jtpay.com/pay/form/PullPay";
    private String DownLaodAddress = "http://www.jtpay.com/apk/wallet.apk";

    private String TestUrl1 = "http://test.jtpay.com:8999/WebForm1.aspx";//waponestepurl
    private String TestUrl2 = "http://test.jtpay.com:8999/WebForm2.aspx";//wapurl
    private String TestUrl3 = "http://test.jtpay.com:8999/WebForm3.aspx";//sdkurl
    private String TestUrl4 = "http://test.jtpay.com:8999/WebForm4.aspx";//sdkonestepurl

    private static String payType="3" ; // 
   
    // Use this for initialization
    void Start()
    {
        //get Android Java interface 
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

        //获取屏幕宽高
        w = Screen.width;
        h = Screen.height;


        //找到button按钮
        btn_changePayType = GameObject.Find("changePayType").GetComponent<Button>();
        btn_wap = GameObject.Find("wappay").GetComponent<Button>();
        btn_sdk = GameObject.Find("sdkpay").GetComponent<Button>();
        btn_wap_onestep = GameObject.Find("wappayOnestep").GetComponent<Button>();
        btn_sdk_onestep = GameObject.Find("sdkpayOnestep").GetComponent<Button>();
        btn_wap_url = GameObject.Find("wappayurl").GetComponent<Button>();
        btn_sdk_url = GameObject.Find("sdkpayurl").GetComponent<Button>();
        btn_wap_onestep_url = GameObject.Find("wappayOnestepurl").GetComponent<Button>();
        btn_sdk_onestep_url = GameObject.Find("sdkpayOnestepurl").GetComponent<Button>();
        btn_payBrower = GameObject.Find("payBrower").GetComponent<Button>();

        //退出按钮
        Button exit_btn = GameObject.Find("exit").GetComponent<Button>();
        exit_btn.transform.position = new Vector3(w - 40, 40);
        text_PayType = GameObject.Find("labelPayType").GetComponent<Text>();
        labelUrlType = GameObject.Find("labelUrlType").GetComponent<Text>();
        labelPayStatus = GameObject.Find("labelPayStatus").GetComponent<Text>();

    }

    // Update is called once per frame
    void Update()
    {

    }

    //wappay
    public void wappay()
    {
        jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

        /** very not recommended for comKey stored on the client */
        String comKey = "40F4A509B7459D667B68C9A51A4E14DA";

        String orderTime= DateTime.Now.ToString("yyyyMMddHHmmss"); 
        String p1_usercode = "10208331";//
        String p2_order = "TestOrder_" + orderTime;
        String p3_money = "0.1";
        String p4_returnurl = "http://pay.jtpay.com/Form/TestReturn";
        String p5_notifyurl = "http://pay.jtpay.com/Form/TestNotice";
        String p6_ordertime = orderTime; 
        /*
        *It is highly recommended that the p7_sign data signature process be completed on the server side,
        *very not recommended for merchant payment keys stored on the client
        *demo as a result of the show, in the mobile terminal
        * */
        String p7_sign = Assets.Md5.UserMd5(p1_usercode + "&" + p2_order + "&" + p3_money +
                "&" + p4_returnurl + "&" + p5_notifyurl + "&" + p6_ordertime + comKey);
        String p9_paymethod = "wap";

        String p14_customname = "name";
        String p17_customip = "ip";
        String p18_product = "testName";
        String p25_terminal = "3";
        String p26_iswappay = "5";

        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
        buffer.Append(BaseApi);
        buffer.Append("?p1_usercode=");
        buffer.Append(p1_usercode);
        buffer.Append("&p2_order=");
        buffer.Append(p2_order);
        buffer.Append("&p3_money=");
        buffer.Append(p3_money);
        buffer.Append("&p4_returnurl=");
        buffer.Append(p4_returnurl);
        buffer.Append("&p5_notifyurl=");
        buffer.Append(p5_notifyurl);
        buffer.Append("&p6_ordertime=");
        buffer.Append(p6_ordertime);
        buffer.Append("&p7_sign=");
        buffer.Append(p7_sign);
        buffer.Append("&p9_paymethod=");
        buffer.Append(p9_paymethod);
        buffer.Append("&p14_customname=");
        buffer.Append(p14_customname);
        buffer.Append("&p17_customip=");
        buffer.Append(p17_customip);
        buffer.Append("&p18_product=");
        buffer.Append(p18_product);
        buffer.Append("&p25_terminal=");
        buffer.Append(p25_terminal);
        buffer.Append("&p26_iswappay=");
        buffer.Append(p26_iswappay);
        /*
        * Above for the need to wear the parameters, the following comment out the part of the
        * optional parameters can be added according to demand.
        * Optional parameter definitions Please refer to the documentation for 26 access parameter
        * descriptions
        */
        /*buffer.Append("&p8_signtype=");
        buffer.Append(p8_signtype);//user define
        buffer.Append("&p10_paychannelnum=");
        buffer.Append(p10_paychannelnum);//user define
        buffer.Append("&p11_cardtype=");
        buffer.Append(p11_cardtype);//user define
        buffer.Append("&p12_channel=");
        buffer.Append(p12_channel);//user define
        buffer.Append("&p13_orderfailertime=");
        buffer.Append(p13_orderfailertime);//user define
        buffer.Append("&p15_customcontacttype=");
        buffer.Append(p15_customcontacttype);//user define
        buffer.Append("&p16_customcontact=");
        buffer.Append(p16_customcontact);//user define
        buffer.Append("&p19_productcat=");
        buffer.Append(p19_productcat);//user define
        buffer.Append("&p20_productnum=");
        buffer.Append(p20_productnum);//user define
        buffer.Append("&p21_pdesc=");
        buffer.Append(p21_pdesc);//user define
        buffer.Append("&p22_version=");
        buffer.Append(p22_version);//user define
        buffer.Append("&p23_charset=");
        buffer.Append(p23_charset);//user define
        buffer.Append("&p24_remark=");
        buffer.Append(p24_remark);//user define*/
        String url = buffer.ToString();
        jo.Call("wapPay", new object[] { url });

    }
    //sdkpay
    public void sdkpay()
    {
        jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

        //todo not recommended for comKey stored on the client
        String comKey = "40F4A509B7459D667B68C9A51A4E14DA";
        String orderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        String p1_usercode = "10208331";
        String p2_order = "TestOrder_" + orderTime;
        String p3_money = "0.1";
        String p4_returnurl = "http://pay.jtpay.com/Form/TestReturn";
        String p5_notifyurl = "http://pay.jtpay.com/Form/TestNotice";
        String p6_ordertime = orderTime;
        /*
        *It is highly recommended that the p7_sign data signature process be completed on the server side,
        *very not recommended for merchant payment keys stored on the client
        *demo as a result of the show, in the mobile terminal
        * */
        String p7_sign = Assets.Md5.UserMd5(p1_usercode + "&" + p2_order + "&" + p3_money +
                "&" + p4_returnurl + "&" + p5_notifyurl + "&" + p6_ordertime + comKey);
        String p18_product = "testName";
        String appid = "20161111102907658374";
        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
        buffer.Append(BaseApi);
        buffer.Append("?p1_usercode=");
        buffer.Append(p1_usercode);
        buffer.Append("&p2_order=");
        buffer.Append(p2_order);
        buffer.Append("&p3_money=");
        buffer.Append(p3_money);
        buffer.Append("&p4_returnurl=");
        buffer.Append(p4_returnurl);
        buffer.Append("&p5_notifyurl=");
        buffer.Append(p5_notifyurl);
        buffer.Append("&p6_ordertime=");
        buffer.Append(p6_ordertime);
        buffer.Append("&p7_sign=");
        buffer.Append(p7_sign);
        buffer.Append("&p9_paymethod=");
        buffer.Append("sdk");
        buffer.Append("&p18_product=");
        buffer.Append(p18_product);
        buffer.Append("&ext_appid=");
        buffer.Append(appid);
        
        jo.Call("sdkPay", new object[] { buffer.ToString()});

    }
    //wappayOnestep
    public void wappayOnestep()
    {
        jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

        /** very not recommended for comKey stored on the client */
        String comKey = "40F4A509B7459D667B68C9A51A4E14DA";

        String orderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        String p1_usercode = "10208331";//
        String p2_order = "TestOrder_" + orderTime;
        String p3_money = "0.1";
        String p4_returnurl = "http://pay.jtpay.com/Form/TestReturn";
        String p5_notifyurl = "http://pay.jtpay.com/Form/TestNotice";
        String p6_ordertime = orderTime;
        /*
        *It is highly recommended that the p7_sign data signature process be completed on the server side,
        *very not recommended for merchant payment keys stored on the client
        *demo as a result of the show, in the mobile terminal
        * */
        String p7_sign = Assets.Md5.UserMd5(p1_usercode + "&" + p2_order + "&" + p3_money +
                "&" + p4_returnurl + "&" + p5_notifyurl + "&" + p6_ordertime + comKey);
        String p9_paymethod = payType;

        String p14_customname = "name";
        String p17_customip = "ip";
        String p18_product = "testName";
        String p25_terminal = "3";
        String p26_iswappay = "5";

        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
        buffer.Append(BaseApi);
        buffer.Append("?p1_usercode=");
        buffer.Append(p1_usercode);
        buffer.Append("&p2_order=");
        buffer.Append(p2_order);
        buffer.Append("&p3_money=");
        buffer.Append(p3_money);
        buffer.Append("&p4_returnurl=");
        buffer.Append(p4_returnurl);
        buffer.Append("&p5_notifyurl=");
        buffer.Append(p5_notifyurl);
        buffer.Append("&p6_ordertime=");
        buffer.Append(p6_ordertime);
        buffer.Append("&p7_sign=");
        buffer.Append(p7_sign);
        buffer.Append("&p9_paymethod=");
        buffer.Append(p9_paymethod);
        buffer.Append("&p14_customname=");
        buffer.Append(p14_customname);
        buffer.Append("&p17_customip=");
        buffer.Append(p17_customip);
        buffer.Append("&p18_product=");
        buffer.Append(p18_product);
        buffer.Append("&p25_terminal=");
        buffer.Append(p25_terminal);
        buffer.Append("&p26_iswappay=");
        buffer.Append(p26_iswappay);
        /*
        * Above for the need to wear the parameters, the following comment out the part of the
        * optional parameters can be added according to demand.
        * Optional parameter definitions Please refer to the documentation for 26 access parameter
        * descriptions
        */
        /*buffer.Append("&p8_signtype=");
        buffer.Append(p8_signtype);//user define
        buffer.Append("&p10_paychannelnum=");
        buffer.Append(p10_paychannelnum);//user define
        buffer.Append("&p11_cardtype=");
        buffer.Append(p11_cardtype);//user define
        buffer.Append("&p12_channel=");
        buffer.Append(p12_channel);//user define
        buffer.Append("&p13_orderfailertime=");
        buffer.Append(p13_orderfailertime);//user define
        buffer.Append("&p15_customcontacttype=");
        buffer.Append(p15_customcontacttype);//user define
        buffer.Append("&p16_customcontact=");
        buffer.Append(p16_customcontact);//user define
        buffer.Append("&p19_productcat=");
        buffer.Append(p19_productcat);//user define
        buffer.Append("&p20_productnum=");
        buffer.Append(p20_productnum);//user define
        buffer.Append("&p21_pdesc=");
        buffer.Append(p21_pdesc);//user define
        buffer.Append("&p22_version=");
        buffer.Append(p22_version);//user define
        buffer.Append("&p23_charset=");
        buffer.Append(p23_charset);//user define
        buffer.Append("&p24_remark=");
        buffer.Append(p24_remark);//user define*/
        String url = buffer.ToString();
        jo.Call("wapPay", new object[] { url });

    }
    //sdkpayOnestep
    public void sdkpayOnestep()
    {
        jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

        //todo not recommended for comKey stored on the client
        String comKey = "40F4A509B7459D667B68C9A51A4E14DA";
        String orderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
        String p1_usercode = "10208331";
        String p2_order = "TestOrder_" + orderTime;
        String p3_money = "0.1";
        String p4_returnurl = "http://pay.jtpay.com/Form/TestReturn";
        String p5_notifyurl = "http://pay.jtpay.com/Form/TestNotice";
        String p6_ordertime = orderTime;
        /*
        *It is highly recommended that the p7_sign data signature process be completed on the server side,
        *very not recommended for merchant payment keys stored on the client
        *demo as a result of the show, in the mobile terminal
        * */
        String p7_sign = Assets.Md5.UserMd5(p1_usercode + "&" + p2_order + "&" + p3_money +
                "&" + p4_returnurl + "&" + p5_notifyurl + "&" + p6_ordertime + comKey);
        String p18_product = "testName";
        String appid = "20161111102907658374";
        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
        buffer.Append(BaseApi);
        buffer.Append("?p1_usercode=");
        buffer.Append(p1_usercode);
        buffer.Append("&p2_order=");
        buffer.Append(p2_order);
        buffer.Append("&p3_money=");
        buffer.Append(p3_money);
        buffer.Append("&p4_returnurl=");
        buffer.Append(p4_returnurl);
        buffer.Append("&p5_notifyurl=");
        buffer.Append(p5_notifyurl);
        buffer.Append("&p6_ordertime=");
        buffer.Append(p6_ordertime);
        buffer.Append("&p7_sign=");
        buffer.Append(p7_sign);
        buffer.Append("&p9_paymethod=");
        buffer.Append("sdk");
        buffer.Append("&p18_product=");
        buffer.Append(p18_product);
        buffer.Append("&ext_appid=");
        buffer.Append(appid);
        //one step pay need ext_paytype
        
        buffer.Append("&ext_paytype=");
        buffer.Append(payType);
        
        jo.Call("sdkPay", new object[] { buffer.ToString() });

    }
    //sdkpayUrl
    public void wappayUrl()
    {
        if (labelUrlType.text.Equals("浏览器"))
        {
            jo.Call("payBrower", new object[] { TestUrl2 });
        }
        else {
            jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

            jo.Call("wapPay", new object[] { TestUrl2 });
        } 

    }
    //sdkpayOnestepUrl
    public void wappayOnestepUrl()
    {
        if (labelUrlType.text.Equals("浏览器"))
        {
            jo.Call("payBrower", new object[] { TestUrl1 });
        }
        else
        {
            jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

            jo.Call("wapPay", new object[] { TestUrl1 });
        }

    }
    //sdkpayUrl
    public void sdkpayUrl()
    {
        if (labelUrlType.text.Equals("浏览器"))
        {
            jo.Call("payBrower", new object[] { TestUrl3 });
        }
        else
        {
            jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

            jo.Call("sdkPay", new object[] { TestUrl3 });
        }

    }
    //sdkpayOnestepUrl
    public void sdkpayOnestepUrl()
    {
        if (labelUrlType.text.Equals("浏览器"))
        {
            jo.Call("payBrower", new object[] { TestUrl4 });
        }
        else
        {
            jo.Call("setDownloadAddress", new object[] { DownLaodAddress });

            jo.Call("sdkPay", new object[] { TestUrl4 });
        }

    }
    //payBrower
    public void payBrower()
    {
        if (labelUrlType.text.Equals("浏览器"))
        {
            labelUrlType.text = "非浏览器";
        }
        else
        {
            labelUrlType.text = "浏览器";
        }
      //  jo.Call("payBrower", new object[] { TestUrl1 });

    }
    //changePayType
    public void changePayType()
    {
        
        if (text_PayType.text.Equals("微信"))
        {
            text_PayType.text = "支付宝";
            payType = "4";
        }
        else
        {
            text_PayType.text = "微信";
            payType = "3";
        }

    }
    public void payStatusU3d(String str) {
        //if (labelPayStatus.text.Length>50) {
          //  labelPayStatus.text = "Sdk支付结果";
        //}
        labelPayStatus.text +="|"+ str;
       
    }

    //退出方法
    public void Exit()
    {//win和android 有效
        Application.Quit();
    }
}
